﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PromotionApp.Model;
using PromotionApp.Utility;
using PromotionApp.BLL;
using System.Web.UI.HtmlControls;

namespace PromotionApp.UserControl
{
    public partial class ucReply : System.Web.UI.UserControl
    {
        #region Property

        public bool IsBackend
        {
            get
            {
                bool isBackend = false;

                if (ViewState["IsBackend"] != null)
                    isBackend = Convert.ToBoolean(ViewState["IsBackend"]);

                return isBackend;
            }
            set
            {
                ViewState["IsBackend"] = value;
            }
        }

        public int TopicId
        {
            get
            {
                int topicId = 0;

                if (ViewState["TopicId"] != null)
                    topicId = Convert.ToInt32(ViewState["TopicId"]);

                return topicId;
            }
            set
            {
                ViewState["TopicId"] = value;
            }
        }

        public int ReplyId
        {
            get
            {
                int replyId = 0;

                if (ViewState["ReplyId"] != null)
                    replyId = Convert.ToInt32(ViewState["ReplyId"]);

                return replyId;
            }
            set
            {
                ViewState["ReplyId"] = value;
            }
        }

        public InternalUser User
        {
            get
            {
                InternalUser user = new InternalUser();

                if (ViewState["InternalUser"] != null)
                    user = ViewState["InternalUser"] as InternalUser;

                return user;
            }
            set
            {
                ViewState["InternalUser"] = value;
            }
        }

        #endregion

        #region Private Variable

        private int _Count = 1;

        #endregion

        #region Public Method

        public void Intialize()
        {
            List<Reply> list = null;

            if (this.IsBackend)
                list = ReplyBLL.GetInstance().GetReplyById(this.ReplyId);
            else
                list = ReplyBLL.GetInstance().GetRepliesByTopicIdAndStatus(this.TopicId, "E");

            this.rptReply.DataSource = list;
            this.rptReply.DataBind();
        }

        private void Delete(int replyId)
        {
            ReplyBLL.GetInstance().DeleteReply(replyId, this.User.UserId);
            Intialize();
        }

        private void ChangeStatus(int replyId, string strStatusCode)
        {
            if (strStatusCode.Equals("E"))
                ReplyBLL.GetInstance().PendingReply(replyId, this.User.UserId);
            else if (strStatusCode.Equals("P"))
                ReplyBLL.GetInstance().EnableReply(replyId, this.User.UserId);

            Intialize();
        }

        private void SetPostedBy(Label lblPostedBy, Reply reply)
        {
            if (reply.UserName.Length > 0)
                lblPostedBy.Text = reply.UserName;
            else if (reply.Email.Length > 0)
                lblPostedBy.Text = reply.Email;
        }

        private void SetImage(Image imgReply, HtmlTableRow trImage, Reply reply)
        {
            if (reply.ImagePath.Length > 0)
                imgReply.ImageUrl = reply.ImagePath;
            else
                trImage.Visible = false;
        }

        private void SetStatus(Label lblStatusDesc, Button btnAction, Button btnDelete, Reply reply)
        {
            if (this.IsBackend)
            {
                if (reply.StatusCode.Equals("E"))
                {
                    lblStatusDesc.Text = "Enable";
                    btnAction.CssClass = "btnTurnOff";
                    btnAction.ToolTip = "[Disable this topic]";
                    btnAction.OnClientClick = "return confirm('Are you sure to want to turn off this topic ?')";
                }
                else if (reply.StatusCode.Equals("P"))
                {
                    lblStatusDesc.Text = "Disable";
                    btnAction.CssClass = "btnTurnOn";
                    btnAction.ToolTip = "[Enable this topic]";
                    btnAction.OnClientClick = "return confirm('Are you sure to want to turn on this topic ?')";
                }
                else if (reply.StatusCode.Equals("D"))
                {
                    lblStatusDesc.Text = "Delete";
                    btnAction.Visible = false;
                    btnDelete.Visible = false;
                }
            }
        }

        #endregion

        #region Event

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                try
                {
                    Intialize();
                }
                catch (Exception ex)
                {
                    PageHelper.AlertMessage(ex.Message, this.Page);
                }
            }
        }

        #endregion

        protected void rptReply_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Label lblMessage = e.Item.FindControl("lblMessage") as Label;
                Image imgReply = e.Item.FindControl("imgReply") as Image;
                Label lblPostedBy = e.Item.FindControl("lblPostedBy") as Label;
                Label lblPostedDate = e.Item.FindControl("lblPostedDate") as Label;
                Label lblStatusDesc = e.Item.FindControl("lblStatusDesc") as Label;
                Button btnAction = e.Item.FindControl("btnAction") as Button;
                Button btnDelete = e.Item.FindControl("btnDelete") as Button;
                Control c = e.Item.FindControl("trImage");
                HtmlTableRow trImage = e.Item.FindControl("trImage") as HtmlTableRow;
                HtmlTableRow trStatus = e.Item.FindControl("trStatus") as HtmlTableRow;
                Label lblReplyNo = e.Item.FindControl("lblReplyNo") as Label;
                Reply reply = e.Item.DataItem as Reply;

                lblMessage.Text = reply.Message;
                lblPostedDate.Text = reply.PostedDate.ToString(ConfigurationHelper.DateTimeDisplayFormat);
                trStatus.Visible = this.IsBackend ? true : false;
                btnAction.CommandArgument = string.Format("{0}|{1}", reply.ReplyId, reply.StatusCode);
                btnDelete.CommandArgument = reply.ReplyId.ToString();
                SetPostedBy(lblPostedBy, reply);
                SetImage(imgReply, trImage, reply);
                SetStatus(lblStatusDesc, btnAction, btnDelete, reply);
                lblReplyNo.Text = this._Count.ToString();
                this._Count += 1;
            }
        }

        protected void rptReply_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            try
            {
                if (e.CommandName.Equals("DeleteReply"))
                    Delete(Convert.ToInt32(e.CommandArgument));
                else
                {
                    string[] param = e.CommandArgument.ToString().Trim().Split('|');
                    ChangeStatus(Convert.ToInt32(param[0]), param[1]);
                }
            }
            catch (Exception ex)
            {
                PageHelper.AlertMessage(ex.Message, this.Page);
            }
        }
    }
}